<?php

/**
 * @author nthai
 * @copyright 2010
 */
?>
<?php
class BackupsController extends AppController {
	var $name = 'Backups';
	var $uses = array('Nongho','Sotay');
	var $helpers = array('Html', 'Form',  'Ajax', 'Xls', 'Cryptastic');
	var $components = array('Session', 'Cookie');
//Xls nam o view/helper/xls    
    function index(){
        	            
    } 
    function index_bayden(){
        	            
    } 
     
    function index_nhst(){
        	            
    } 
    function index_phieudt(){
        	            
    }
    function index_sd_tt(){
        	            
    }
    function local_bayden(){
        $cb = $this->User->find('first',
                array('conditions'=>array('User.id'=>$this->Auth->user('id'))));
        
        $this->data['Backup']['ngaybd'] = $_POST['ngaybd'];
        $this->data['Backup']['ngaykt'] = $_POST['ngaykt']; 
            
      if($this->data['Backup']['full']==1){
           
        
           

            $pdtbds=$this->Nongho->query(' select * from p_dt_bds   where canbo_id='.$cb['User']['canbo_id']);
          //  $kqbds=$this->Nongho->query(' select * from kq_bds');

                   
       // $tstts=$this->Nongho->query(' select * from ts_tts');
       }
       ELSE              {
            $pdtbds=$this->Nongho->query(" select * from p_dt_bds   where canbo_id=".$cb['User']['canbo_id']." and
                        date(date_sua)>='".$this->data['Backup']['ngaybd']."'  
                        and date(date_sua)<='".$this->data['Backup']['ngaykt']."'");
         
       }
      
     
      
      $bien='';
       //LAY CAC KET QUA CUA phieu bay den
      $n=count($pdtbds);
      
      //Neu cac dong ket qua tu phieu moi xuat ra
      if($n!=0){
            //cong cac chuoi cua id thoitiet
            for($i=0;$i<$n;$i++){
                    if($i!=0)
                        $bien=$bien.", ";
                         $bien=$bien.$pdtbds[$i]['p_dt_bds']['id'];
            }
            
            $kqbds=$this->Nongho->query("select * from kq_bds
                    where p_dt_bd_id in  (".$bien.")");
                         
            }
            
      //neu bang 0 thi khong co dong nao tra ve
      else {
            $kqbds=$this->Nongho->query("select * from kq_bds
                    where p_dt_bd_id ='dfjhf'");
             
      }
      
      //GHI NHAT KY VAO CSDL
       $bk=$this->Nongho->query("INSERT INTO bks 
                    (canbo_id, date_bk, loai_bk,ten_bang,bd,kt) 
                    VALUES ( ".$cb['User']['canbo_id'].", CURRENT_TIMESTAMP, '".$this->data['Backup']['full']."'
                            ,'p_bayden,ketquabd',
                            '".$this->data['Backup']['ngaybd']."','".$this->data['Backup']['ngaykt']. "')");
      
      $this->layout='';
      
      $this->set(compact('kqbds','pdtbds','cb'));
    }
    
    
    //backup gom: Nongho, sotay
    function local_nhst(){

        $cb = $this->User->find('first',
                array('conditions'=>array('User.id'=>$this->Auth->user('id'))));
        $this->data['Backup']['ngaybd'] = $_POST['ngaybd'];
            $this->data['Backup']['ngaykt'] = $_POST['ngaykt']; 
            
      if($this->data['Backup']['full']==1){
            $sotays=$this->Nongho->query(' select * from sotays');
            
            $nonghos=$this->Nongho->query(' select * from nonghos');

        }
       ELSE
       {
            
            
             $sotays=$this->Nongho->query(" select * from sotays
             where  date(date_sua)>='".$this->data['Backup']['ngaybd']."'  
                        and date(date_sua)<='".$this->data['Backup']['ngaykt']."'");
           

            $nonghos=$this->Nongho->query(" select * from nonghos
           where  date(date_sua)>='".$this->data['Backup']['ngaybd']."'  
                        and date(date_sua)<='".$this->data['Backup']['ngaykt']."'");
           
       }
      
      
           
      //GHI NHAT KY VAO CSDL
      $bk=$this->Nongho->query("INSERT INTO bks 
                    (canbo_id, date_bk, loai_bk,ten_bang,bd,kt) 
                    VALUES ( ".$cb['User']['canbo_id'].", CURRENT_TIMESTAMP, '".$this->data['Backup']['full']."'
                            ,'Nongho, sotay',
                            '".$this->data['Backup']['ngaybd']."','".$this->data['Backup']['ngaykt']. "')");
                            
      
      $this->layout='';
      
      $this->set(compact('nonghos', 'sotays','cb'));
          
    }
    
    
    function bangcon($biencha,$biencon,$t_cha,$t_con,$fk_cha){
        $n=0; $bien='';
        $n=count($biencha);
      
      //Neu cac dong ket qua tu phieu moi xuat ra
        if($n!=0){
            //cong cac chuoi cua id thoitiet
            for($i=0;$i<$n;$i++){
                    if($i!=0)
                        $bien=$bien.", ";
                         $bien=$bien.$biencha[$i][$t_cha]['id'];
            }
            
            $biencon=$this->Nongho->query("select * from ".$t_con."
                    where ".$fk_cha." in  (".$bien.")");
                         
            }
            
      //neu bang 0 thi khong co dong nao tra ve
      else {
           $biencon=$this->Nongho->query("select * from ".$t_con."
                    where ".$fk_cha."='dfjhf'");
             
      } 
    }
    
    
    //backup dvtinh,sudungphan,thuoc,thoitiet
    function local_sd_tt(){

    
    
    $this->data['Backup']['ngaybd'] = $_POST['ngaybd'];
    $this->data['Backup']['ngaykt'] = $_POST['ngaykt']; 
        $cb = $this->User->find('first',
                array('conditions'=>array('User.id'=>$this->Auth->user('id'))));
        
      if($this->data['Backup']['full']==1){
            
            $dvtinhs=$this->Nongho->query('select * from dvtinhs');
        
            $sdphanbons=$this->Nongho->query(' select * from sd_phanbons');
            $sdungthuocs=$this->Nongho->query(' select * from sdung_thuocs');
            $thoitiets=$this->Nongho->query(' select * from thoitiets where canbo_id='.$cb['User']['canbo_id']);
        
       // $tstts=$this->Nongho->query(' select * from ts_tts');
       }
       ELSE    
            {
            //$this->data['Backup']['ngaybd'] = $_POST['ngaybd'];
            //$this->data['Backup']['ngaykt'] = $_POST['ngaykt']; 
            
            $dvtinhs=$this->Nongho->query("select * from dvtinhs
             where  date(date_sua)>='".$this->data['Backup']['ngaybd']."'  
                        and date(date_sua)<='".$this->data['Backup']['ngaykt']."'");
        
            $sdphanbons=$this->Nongho->query(" select * from sd_phanbons
             where  date(date_sua)>='".$this->data['Backup']['ngaybd']."'  
                        and date(date_sua)<='".$this->data['Backup']['ngaykt']."'");
            $sdungthuocs=$this->Nongho->query("select * from sdung_thuocs
             where  date(date_sua)>='".$this->data['Backup']['ngaybd']."'  
                        and date(date_sua)<='".$this->data['Backup']['ngaykt']."'");
       
            $thoitiets=$this->Nongho->query(" select * from thoitiets where canbo_id=".$cb['User']['canbo_id']." and
                        date(date_sua)>='".$this->data['Backup']['ngaybd']."'  
                        and date(date_sua)<='".$this->data['Backup']['ngaykt']."'");
       }
      
      
      
      $bien='';
      //LAY CAC KET QUA CUA THOI TIET
      $n=count($thoitiets);
      
      //Neu cac dong ket qua tu phieu moi xuat ra
      if($n!=0){
            //cong cac chuoi cua id thoitiet
            for($i=0;$i<$n;$i++){
                    if($i!=0)
                        $bien=$bien.", ";
                         $bien=$bien.$thoitiets[$i]['thoitiets']['id'];
            }
            
            $tstts=$this->Nongho->query("select * from ts_tts
                    where thoitiet_id in  (".$bien.")");
                         
            }
            
      //neu bang 0 thi khong co dong nao tra ve
      else {
            $tstts=$this->Nongho->query("select * from ts_tts
                    where thoitiet_id ='dfjhf'");
             
      }
      
      //GHI NHAT KY VAO CSDL
      $bk=$this->Nongho->query("INSERT INTO bks 
                    (canbo_id, date_bk, loai_bk,ten_bang,bd,kt) 
                    VALUES ( ".$cb['User']['canbo_id'].", CURRENT_TIMESTAMP, '".$this->data['Backup']['full']."'
                            ,'sdungphan-thuoc, thoitiet,donvitinh',
                            '".$this->data['Backup']['ngaybd']."','".$this->data['Backup']['ngaykt']. "')");
      
      $this->layout='';
      
      $this->set(compact('cb','sdphanbons', 'sdungthuocs', 
                    'thoitiets', 'tstts','dvtinhs'));
          
    }
    
    
    
    function local_phieudt(){
        
    $this->data['Backup']['ngaybd'] = $_POST['ngaybd'];
    $this->data['Backup']['ngaykt'] = $_POST['ngaykt']; 

        $cb = $this->User->find('first',
                array('conditions'=>array('User.id'=>$this->Auth->user('id'))));
        
      if($this->data['Backup']['full']==1){
           
            $phieudts=$this->Nongho->query(' select * from phieu_dts where canbo_id='.$cb['User']['canbo_id']);
        //$ketquas=$this->Nongho->query('select * from ketquas');

       }
       ELSE              {
            //$this->data['Backup']['ngaybd'] = $_POST['ngaybd'];
            //$this->data['Backup']['ngaykt'] = $_POST['ngaykt']; 
            
                    
            $phieudts=$this->Nongho->query(" select * from phieu_dts where canbo_id=".$cb['User']['canbo_id']." and
                        date(date_sua)>='".$this->data['Backup']['ngaybd']."'  
                        and date(date_sua)<='".$this->data['Backup']['ngaykt']."'");
      
       }
      
      
      //LAY KET QUA CUA PHIEU DIEU TRA
      $bien='';
      $n=count($phieudts);
      
      //Neu cac dong ket qua tu phieu moi xuat ra
      if($n!=0){
            for($i=0;$i<$n;$i++){
                    if($i!=0)
                        $bien=$bien.", ";
                         $bien=$bien.$phieudts[$i]['phieu_dts']['id'];
            }
            
            $ketquas=$this->Nongho->query("select * from ketquas
                    where phieu_dt_id in  (".$bien.")");
                         
            }
            
      //neu bang 0 thi khong co dong nao tra ve
      else {
            $ketquas=$this->Nongho->query("select * from ketquas
                    where phieu_dt_id ='dfjhf'");
            
      }
      
           
      //GHI NHAT KY VAO CSDL
      $bk=$this->Nongho->query("INSERT INTO bks 
                    (canbo_id, date_bk, loai_bk,ten_bang,bd,kt) 
                    VALUES ( ".$cb['User']['canbo_id'].", CURRENT_TIMESTAMP, '".$this->data['Backup']['full']."'
                            ,'phieudt',
                            '".$this->data['Backup']['ngaybd']."','".$this->data['Backup']['ngaykt']. "')");
      
      $this->layout='';
      
      $this->set(compact('cb','ketquas','kqbds','nonghos', 'pdtbds', 'phieudts',
          'sdphanbons', 'sdungthuocs', 'sotays', 'thoitiets', 'tstts','dvtinhs'));
          
    }
    
    function server_backup(){
    if(!empty($this->data)){
        
    	$cb = $this->User->find('first',
                array('conditions'=>array('User.id'=>$this->Auth->user('id'))));
        $dvtinhs=$this->Nongho->query(' select * from dvtinhs');
        print_r($dvtinhs);
        $this->data['Backup']['ngaybd'] = $_POST['ngaybd'];
        $this->data['Backup']['ngaykt'] = $_POST['ngaykt']; 
	   if($this->data['Backup']['full']==1){
            $canbos=$this->Nongho->query('select * from canbos ');
            $chicucbvtvs=$this->Nongho->query('select * from chicuc_bvtvs ');
            $diemdts=$this->Nongho->query('select * from diem_dts ');
            $doituongs=$this->Nongho->query('select doituongs.* from doituongs ');
            $dtloaidts=$this->Nongho->query('select dt_loaidts.* from dt_loaidts ');
            $giongluas=$this->Nongho->query('select giongluas.* from giongluas ');
            $gocthuocs=$this->Nongho->query('select gocthuocs.* from gocthuocs ');
            $hcthuocs=$this->Nongho->query('select hc_thuocs.* from hc_thuocs ');
            $hoatchats=$this->Nongho->query('select hoatchats.* from hoatchats ');
            $huyens=$this->Nongho->query('select * from huyens ');
            $loaidts=$this->Nongho->query('select loai_dts.* from loai_dts ');
            $loaiphans=$this->Nongho->query('select loaiphans.* from loaiphans ');
            $muavues=$this->Nongho->query('select mua_vues.* from mua_vues ');
            $nams=$this->Nongho->query('select nams.* from nams ');
            $nddts=$this->Nongho->query('select nd_dts.* from nd_dts ');
            $nsses=$this->Nongho->query('select nsses.* from nsses ');
            $thongsos=$this->Nongho->query('select thongsos.* from thongsos ');
            $thuocs=$this->Nongho->query('select thuocs.* from thuocs ');
            $tinhs=$this->Nongho->query('select tinhs.* from tinhs ');
            $trambvtvs=$this->Nongho->query('select trambvtvs.* from trambvtvs ');
            $users=$this->Nongho->query('select users.* from users ');
            $xas=$this->Nongho->query('select xas.* from xas ');

      /*      $bk=$this->Nongho->query("INSERT INTO bkas 
               (canbo_id, date_bk, loai_bk) 
                    VALUES ( ".$cb['User']['canbo_id'].", CURRENT_TIMESTAMP, '1')");
        */           
        }
        else{
            
          
           $canbos=$this->Nongho->query('select canbos.* from canbos 
                    where  date(date_sua)>='.$this->data['Backup']['ngaybd'].'  
                        and date(date_sua)<='.$this->data['Backup']['ngaykt']);
           $chicucbvtvs=$this->Nongho->query('select chicuc_bvtvs.* from chicuc_bvtvs 
                    where  date(date_sua)>='.$this->data['Backup']['ngaybd'].'  
                        and date(date_sua)<='.$this->data['Backup']['ngaykt']);
           $diemdts=$this->Nongho->query('select diem_dts.* from diem_dts 
                    where  date(date_sua)>='.$this->data['Backup']['ngaybd'].'
                      and date(date_sua)<='.$this->data['Backup']['ngaykt']);
           $doituongs=$this->Nongho->query('select doituongs.* from doituongs 
                    where  date(date_sua)>='.$this->data['Backup']['ngaybd'].' 
                         and date(date_sua)<='.$this->data['Backup']['ngaykt']);
           $dtloaidts=$this->Nongho->query('select dt_loaidts.* from dt_loaidts 
                    where  date(date_sua)>='.$this->data['Backup']['ngaybd'].' 
                         and date(date_sua)<='.$this->data['Backup']['ngaykt']);
           $giongluas=$this->Nongho->query('select giongluas.* from giongluas 
                    where  date(date_sua)>='.$this->data['Backup']['ngaybd'].'  
                        and date(date_sua)<='.$this->data['Backup']['ngaykt']);
            $hcthuocs=$this->Nongho->query('select hc_thuocs.* from hc_thuocs 
                    where  date(date_sua)>='.$this->data['Backup']['ngaybd'].'  
                        and date(date_sua)<='.$this->data['Backup']['ngaykt']);
           $hoatchats=$this->Nongho->query('select hoatchats.* from hoatchats 
                    where  date(date_sua)>='.$this->data['Backup']['ngaybd'].'  
                        and date(date_sua)<='.$this->data['Backup']['ngaykt']);
           $huyens=$this->Nongho->query('select huyens.* from huyens 
                    where  date(date_sua)>='.$this->data['Backup']['ngaybd'].'  
                        and date(date_sua)<='.$this->data['Backup']['ngaykt']);
           $loaidts=$this->Nongho->query('select loai_dts.* from loai_dts 
                    where  date(date_sua)>='.$this->data['Backup']['ngaybd'].' 
                         and date(date_sua)<='.$this->data['Backup']['ngaykt']);
            $loaiphans=$this->Nongho->query('select loaiphans.* from loaiphans 
                    where  date(date_sua)>='.$this->data['Backup']['ngaybd'].' 
                         and date(date_sua)<='.$this->data['Backup']['ngaykt']);
           $muavues=$this->Nongho->query('select mua_vues.* from mua_vues 
                    where  date(date_sua)>='.$this->data['Backup']['ngaybd'].' 
                         and date(date_sua)<='.$this->data['Backup']['ngaykt']);
           $nams=$this->Nongho->query('select nams.* from nams 
                    where  date(date_sua)>='.$this->data['Backup']['ngaybd'].'
                          and date(date_sua)<='.$this->data['Backup']['ngaykt']);
            $nddts=$this->Nongho->query('select nd_dts.* from nd_dts 
                    where  date(date_sua)>='.$this->data['Backup']['ngaybd'].'
                          and date(date_sua)<='.$this->data['Backup']['ngaykt']);
            $nsses=$this->Nongho->query('select nsses.* from nsses 
                    where  date(date_sua)>='.$this->data['Backup']['ngaybd'].'
                          and date(date_sua)<='.$this->data['Backup']['ngaykt']);
           $thongsos=$this->Nongho->query('select thongsos.* from thongsos 
                    where  date(date_sua)>='.$this->data['Backup']['ngaybd'].'
                          and date(date_sua)<='.$this->data['Backup']['ngaykt']);
           $thuocs=$this->Nongho->query('select thuocs.* from thuocs 
                   where  date(date_sua)>='.$this->data['Backup']['ngaybd'].'
                          and date(date_sua)<='.$this->data['Backup']['ngaykt']);
           $tinhs=$this->Nongho->query('select tinhs.* from tinhs 
                    where  date(date_sua)>='.$this->data['Backup']['ngaybd'].'
                          and date(date_sua)<='.$this->data['Backup']['ngaykt']);
           $trambvtvs=$this->Nongho->query('select trambvtvs.* from trambvtvs 
                    where  date(date_sua)>='.$this->data['Backup']['ngaybd'].'
                          and date(date_sua)<='.$this->data['Backup']['ngaykt']);
           $users=$this->Nongho->query('select users.* from users 
                    where  date(date_sua)>='.$this->data['Backup']['ngaybd'].' 
                         and date(date_sua)<='.$this->data['Backup']['ngaykt']);
           $xas=$this->Nongho->query('select xas.* from xas 
                    where  date(date_sua)>='.$this->data['Backup']['ngaybd'].'
                          and date(date_sua)<='.$this->data['Backup']['ngaykt']);

          /*
           $bk=$this->Nongho->query("INSERT INTO bkas 
                    (canbo_id, date_bk, loai_bk) 
                    VALUES ( ".$cb['User']['canbo_id'].", CURRENT_TIMESTAMP, '2')");
                    */
        }
        
        $bk=$this->Nongho->query("INSERT INTO bkas 
                    (canbo_id, date_bk, loai_bk,ten_bang,bd,kt) 
                    VALUES ( ".$cb['User']['canbo_id'].", CURRENT_TIMESTAMP, '".$this->data['Backup']['full']."'
                            ,'phieudt',
                            '".$this->data['Backup']['ngaybd']."','".$this->data['Backup']['ngaykt']. "')");
      
        $this->layout='';
        //chu y la bien co dau "_" thi khong NHAN RA DUOC
       $this->set(compact('canbos','chicucbvtvs','diemdts','doituongs',
        'dtloaidts','giongluas','gocthuocs',
            'hcthuocs','hoatchats','huyens','loaidts', 'loaiphans',
                'muavues','nams','nddts','thongsos','nsses','thuocs',
                'tinhs','trambvtvs','users','xas','dvtinhs','cb'));
    }
    else {
        	//$this->Session->setFlash('D? li?u dang r?ng');
            $this->redirect(array('controller'=>'bkas','action'=>'index'));
    }
   } 
   
}